package com.winderinfo.yikaotianxia.core.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaselanFragment extends Fragment {

    public Unbinder unbinder;

    //Fragment的View加载完毕的标记
    private boolean isViewCreated;
    //Fragment对用户可见的标记
    private boolean isUIVisible;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), layoutResID(), null);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }


    protected abstract int layoutResID();

    protected abstract void initView(View view);

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        lazyLoad();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }

    private void lazyLoad() {
        //这里进行双重标记判断,是因为setUserVisibleHint会多次回调,并且会在onCreateView执行前回调,必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isUIVisible) {
            loadData();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isViewCreated = false;
        isUIVisible = false;
    }

    protected abstract void loadData();


    /**
     * 跳转至另一个Activity
     *
     * @param targetActivityClass 目标Activity
     */
    protected void openActivity(Class<?> targetActivityClass) {
        Intent intent = new Intent(getActivity(), targetActivityClass);
        startActivity(intent);
    }

    /**
     * 跳转至另一个Activity
     *
     * @param targetActivityClass 目标Activity
     * @param bundle              传递参数
     */
    protected void openActivity(Class<?> targetActivityClass, Bundle bundle) {
        Intent intent = new Intent(getActivity(), targetActivityClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 跳转至另一个Activity并且关闭当前Activity
     *
     * @param targetActivityClass 目标Activity
     */
    protected void openActivityAndCloseThis(Class<?> targetActivityClass) {
        openActivity(targetActivityClass);
        getActivity().finish();
    }

    /**
     * 跳转至另一个Activity并且关闭当前Activity
     *
     * @param targetActivityClass 目标Activity
     * @param bundle              传递参数
     */
    protected void openActivityAndCloseThis(Class<?> targetActivityClass, Bundle bundle) {
        openActivity(targetActivityClass, bundle);
        getActivity().finish();
    }
}

